"""
Game
The main start-up module for this game.

This front-end is intended to be used primarily by the developers. To that end, 
verbosity is at least as important as user-friendliness, and a GUI is mostly
optional. The end users should NOT use this; a GUI will be developed when the 
engine matures.
"""

import pgEngine, logging

def getPlot(boat, weather):
    print "Plot your move for boat %s at location %s." % (
                boat, pgame.ocean.getCoord(boat))
    print "The wind is from the %s with strength %s" % (
                weather.dir, weather.dist)
    repeat = True
    while repeat:
        #Prompt
        ndir = raw_input('What direction? ')
        dist = int(raw_input('What distance? '))
        again = raw_input('Do another segment? ')
        #Process
        if again[0].lower() in ('y', 't', '1'): repeat = True
        elif again[0].lower() in ('n', 'f', '0'): repeat = False
        odir = pgEngine.directions.Direction(ndir)
        segment = pgEngine.movementData.move(dir=odir, dist=dist)
        blog.debug('Movement plot before: %s#%X' % (boat.plot, id(boat.plot)))
        blog.debug('Segment to add: %s' % segment)

        #Execute
        try:
            boat.plot.append(segment)

        except pgEngine.boat.MovementException:
            print 'You went too far.'
            repeat = True

        except:
            raise
        blog.debug('Movement plot after: %s#%X' % (boat.plot, id(boat.plot)))

if __name__ == "__main__":
    logFormat = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
    logging.basicConfig(level=logging.DEBUG,
                        format=logFormat,
                        datefmt='%d-%m-%y %H:%M',
                        filename='pgameDebugConsole.log',
                        filemode='w')
    #Add a handler
    console = logging.StreamHandler()
    console.setLevel(2)
    consoleFormat = '%(name)-27s %(levelname)-6s %(message)s'
    console.setFormatter( logging.Formatter(consoleFormat) )
    logging.getLogger('').addHandler(console)
    
    blog = logging.getLogger('pgame.UIs.debug')
    
    #Now for the actual game code
    blog.debug('Creating game...')
    pgame = pgEngine.game.PirateGame()
    
    blog.debug('Creating player and setting callbacks')
    blog.debug('Game type: %s' % type(pgame) )
    localPlayer = pgame.addPlayer()
    localPlayer.setCallbacks(plot=getPlot)
    
    blog.debug('Starting game')
    pgame.startGame()
    blog.debug('Entering turn loop')
    while True:
        pgame.doTurn()
